<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 1/19/15
 * Time: 10:48 AM
 */
class SendEmailStaplesTechCommand extends CConsoleCommand {
    public function run($args) {
        $results = Deal::model()->findAllByAttributes(array('type' => DealTypeEnum::staples, 'sent_email' => 0));
        if (count($results) > 0 && count($results) > Yii::app()->params['limit']) {
            $html = '';
            $html .= '<table style="width:100%;" border="1">';
            $html .= '<tr>';
            $html .= '<th>Picture</th>';
            $html .= '<th>Name</th>';
            $html .= '<th>Price</th>';
            $html .= '<th>Link</th>';
            $html .= '</tr>';
            foreach ($results as $result) {
                $result->sent_email = true;
                $result->save();
                $html .= '<tr>';
                if ($result->picture != '') {
                    $html .= '<td style="text-align:center"><img style="max-width: 100px; max-height: 100px" alt="" src="' . $result->picture . '"/> </td>';
                } else {
                    $html .= '<td> </td>';
                }
                $html .= '<td> ' . $result->name . '</td>';
                $html .= '<td>' . $result->price . '</td>';
                $html .= '<td><a href="' . $result->url . '">Link</a></td>';
                $html .= '</tr>';

            }
            $html .= '</table>';

            Mailer::sendEmail(Yii::app()->params['large_email'], "Staple's deals", $html);
        } else {
            foreach ($results as $k => $result) {
                $result->sent_email = true;
                $result->save();
                if ($k > 5) break;
                $html = '';
                $html .= '<p>';
                $html .= "<div>Name: " . $result->name . "</div>";
                $html .= "<div>Price: " . $result->price . "</div>";
                $html .= "<div><a href=" . $result->url . ">Link</a></div>";
                $html .= '</p>';

                Mailer::sendEmail(Yii::app()->params['small_email'], "$result->name | $result->price", $html);
            }
        }
    }
}